from virtual_system import System, analysis

class RaySystem(System):
    # 模仿ray的
    def schedule(self, task):
        # node_index = 0
        
        start_node = task.start_node

        if None in start_node.executing_tasks and start_node.task_queue.empty(): # 自己可以解决
            return start_node.index

        min_timecost_node = None #self.nodes[0]
        min_timecost = 99999999
        for node in self.nodes[1:]:
            node_index = node.index
            hearbeat= self.heartbeats[node_index]

            avg_execution_time  = hearbeat['avg_execution_time']
            queue_size = hearbeat['queue_size']

            time_cost = avg_execution_time * queue_size + task.calNetworkTimeCostTo(node_index)

            if time_cost < min_timecost:
                min_timecost = time_cost
                min_timecost_node = node

        return min_timecost_node.index

if __name__ == '__main__':
    system = RaySystem()
    system.createNodes(5)
    for time_frame in system.time_frames:
        system.step()
        pass

    analysis(system)
    pass